Slide 1

Slide 1 text

JavaScript Promises Thinking Sync in an Async World then

Slide 2

Slide 2 text

Kerrick Long Things I make and do Where to find me online twitter.com/KerrickLong github.com/Kerrick Lead Front-end Developer! at Second Street KerrickLong.com www. meetup.com/STLEmber

Slide 3

Slide 3 text

try {! console.log(getLatestGrade(getStudent(name)));! }! catch (error) {! logError(error);! }! finally {! logOut();! }!

Slide 4

Slide 4 text

try {! console.log(getLatestGrade(getStudent(name)));! }! catch (error) {! logError(error);! }! finally {! logOut();! }! XMLHttpRequest

Slide 5

Slide 5 text

getStudent(name, function(student) {! getLatestGrade(student, function(grade) {! console.log(grade);! logOut();! });! });!

Slide 6

Slide 6 text

var handleError = function(error) {! logError(error);! logOut();! };! getStudent(name, function(error, student) {! if (error) return handleError(error);! getLatestGrade(student, function(error, grade) {! if (error) return handleError(error);! console.log(grade);! logOut();! });! });!

Slide 7

Slide 7 text

var handleError = function(error) {! logError(error);! logOut();! };! getStudent(name, {! error: handleError,! success: function(student) {! getLatestGrade(student, {! error: handleError! success: function(grade) {! console.log(grade);! logOut();! },! })! },! });!

Slide 8

Slide 8 text

try {! console.log(getLatestGrade(getStudent(name)));! }! catch (error) {! logError(error);! }! finally {! logOut();! }! Awesome

Slide 9

Slide 9 text

var handleError = function(error) {! logError(error);! logOut();! };! getStudent(name, {! error: handleError,! success: function(student) {! getLatestGrade(student, {! error: handleError! success: function(grade) {! console.log(grade);! logOut();! },! })! },! });! Awkward.

Slide 10

Slide 10 text

getStudent(name)! .then(getLatestGrade)! .then(console.log)! .catch(logError)! .then(logOut)! ;! Promises

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Promise Basics

Slide 13

Slide 13 text

Promise Basics Consuming Promises

Slide 14

Slide 14 text

Promise Basics Consuming Promises Creating Promises

Slide 15

Slide 15 text

Promise Basics Consuming Promises Creating Promises Advanced Techniques

Slide 16

Slide 16 text

Promise Basics

Slide 17

Slide 17 text

Getting Promises

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

RSVP.js Q.js • Bluebird

Slide 20

Slide 20 text

RSVP.js Q.js • Bluebird Promises/A+

Slide 21

Slide 21 text

Promise Guarantees

Slide 22

Slide 22 text

Always Async

Slide 23

Slide 23 text

Always Async Returns a Promise

Slide 24

Slide 24 text

Always Async Returns a Promise Handled Once

Slide 25

Slide 25 text

Always Async Returns a Promise Handled Once Then’able

Slide 26

Slide 26 text

Promise States

Slide 27

Slide 27 text

Promise States Pending Fulfilled Rejected

Slide 28

Slide 28 text

Promise States Pending Fulfilled Rejected } Settled

Slide 29

Slide 29 text

Promise States Pending Fulfilled Rejected

Slide 30

Slide 30 text

Promise States Pending Fulfilled Rejected Value Reason

Slide 31

Slide 31 text

Promise States Pending Fulfilled Rejected Return Value Thrown Exception

Slide 32

Slide 32 text

Consuming Promises

Slide 33

Slide 33 text

Promise Prototype Methods

Slide 34

Slide 34 text

getJSON('/comments')! .then(onFulfilled, onRejected) Promise.prototype.then

Slide 35

Slide 35 text

getJSON('/comments')! .then(function(comments) {! if (comments) return 'Good'! else throw new Error('Bad')! }, function(reason) {! // handle getJSON errors! }) Promise.prototype.then

Slide 36

Slide 36 text

getJSON('/comments')! .then(fulfillOne, rejectOne)! .then(fulfillTwo)! .then(null, rejectTwo) Promise.prototype.then

Slide 37

Slide 37 text

getJSON('/comments')! .then(fulfillOne, rejectOne)! .then(fulfillTwo)! .then(null, rejectTwo) Promise.prototype.then THROW

Slide 38

Slide 38 text

getJSON('/comments')! .then(fulfillOne, rejectOne)! .then(fulfillTwo)! .then(null, rejectTwo) Promise.prototype.then THROW

Slide 39

Slide 39 text

getJSON onFulfilled onRejected getJSON onFulfilled onRejected

Slide 40

Slide 40 text

getJSON('/comments') // waiting...! .then(extractFirstId)! .then(getCommentById) // waiting...! .then(showComment) Promise.prototype.then

Slide 41

Slide 41 text

var promise = getJSON('/comments');! somethingElse();! promise.then(onFulfilled, onRejected); Promise.prototype.then

Slide 42

Slide 42 text

getJSON('/comments')! .then(null, onRejected) Promise.prototype.catch

Slide 43

Slide 43 text

getJSON('/comments')! .catch(onRejected) Promise.prototype.catch

Slide 44

Slide 44 text

Promise Static Methods

Slide 45

Slide 45 text

Promise.cast('Hi you!')! .then(onFulfilled, onRejected) Promise.cast

Slide 46

Slide 46 text

Promise.cast(seededRandom(42))! .then(onFulfilled, onRejected) Promise.cast

Slide 47

Slide 47 text

Promise.cast($.ajax(config))! .then(onFulfilled, onRejected) Promise.cast

Slide 48

Slide 48 text

$.ajax(config)! .then(onFulfilled, onRejected)

Slide 49

Slide 49 text

$.ajax(config)! .then(onFulfilled, onRejected) Lies, all LIES!

Slide 50

Slide 50 text

Promise.cast($.ajax(config))! .then(onFulfilled, onRejected) Promise.cast

Slide 51

Slide 51 text

var promises = [ getJSON('/a'),! getJSON('/b'), getJSON('/c') ]! ! Promise.all(promises)! .then(allFulfilled, firstRejected) Promise.all

Slide 52

Slide 52 text

var promises = [ getJSON('/a'),! 'Hi you!', 42, getJSON('/c') ]! ! Promise.all(promises)! .then(allFulfilled, firstRejected) Promise.all

Slide 53

Slide 53 text

var promises = [ saveTo(server1),! saveTo(server2), saveTo(server3) ]! ! Promise.race(promises)! .then(firstFulfilled, firstRejected) Promise.race

Slide 54

Slide 54 text

Creating Promises

Slide 55

Slide 55 text

new Promise()

Slide 56

Slide 56 text

function() {! var name = prompt('Your name?')! if (!name)! throw new Error('Rude user!')! else! return name! } new Promise()

Slide 57

Slide 57 text

new Promise(function(fulfill, reject) {! var name = prompt('Your name?')! if (!name)! throw new Error('Rude user!')! else! return name! }) new Promise()

Slide 58

Slide 58 text

new Promise(function(fulfill, reject) {! var name = prompt('Your name?')! if (!name)! reject(new Error('Rude user!'))! else! fulfill(name)! }) new Promise()

Slide 59

Slide 59 text

Shortcuts

Slide 60

Slide 60 text

new Promise(function(fulfill, reject) {! fulfill(something)! }) Promise.resolve()

Slide 61

Slide 61 text

new Promise(function(fulfill, reject) {! fulfill(something)! })! Promise.resolve(something) Promise.resolve()

Slide 62

Slide 62 text

new Promise(function(fulfill, reject) {! reject(something)! }) Promise.reject()

Slide 63

Slide 63 text

new Promise(function(fulfill, reject) {! reject(something)! })! Promise.reject(something) Promise.reject()

Slide 64

Slide 64 text

Advanced Techniques

Slide 65

Slide 65 text

this.get('name') // 'Kerrick'! ! getJSON('/comments')! .then(function(comments) {! this.get('name') // throws! }) this and bind()

Slide 66

Slide 66 text

this.get('name') // 'Kerrick'! var self = this! getJSON('/comments')! .then(function(comments) {! self.get('name') // 'Kerrick'! }) this and bind()

Slide 67

Slide 67 text

this.get('name') // 'Kerrick'! ! getJSON('/comments')! .then(function(comments) {! this.get('name') // 'Kerrick'! }.bind(this)) this and bind()

Slide 68

Slide 68 text

getJSON('/comments')! .then(function(comments) {! throw new Error('Hello?')! }) Absorbed Rejections

Slide 69

Slide 69 text

getJSON('/comments')! .then(function(comments) {! throw new Error('Hello?')! })! .catch(console.error.bind(console)) Absorbed Rejections

Slide 70

Slide 70 text

getStudent(name) // ! .then(getLatestGrade)! .then(log)! .catch(logError)! .then(logOut) Promise-aware Functions

Slide 71

Slide 71 text

log(getLatestGrade(getStudent(name)))! .catch(logError)! .then(logOut) Promise-aware Functions

Slide 72

Slide 72 text

function getLatestGrade(promise) {! return Promise.cast(promise)! .then(function(value) {! // getLatestGrade Logic! })! } Promise-aware Functions